package com.project.ujob.utils;

import java.io.FileInputStream;
import java.io.IOException;

import javax.annotation.PostConstruct;

import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.project.ujob.properties.QiNiuProperties;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

@Component
@ImportAutoConfiguration({ QiNiuProperties.class })
public class QiniuCloudUtil {

	private QiNiuProperties qiNiuProperties;
	private Auth auth;
	private Configuration cfg;

	QiniuCloudUtil(QiNiuProperties qiNiuProperties) {
		this.qiNiuProperties = qiNiuProperties;
	}

	@PostConstruct
	private void init() {
		auth = Auth.create(qiNiuProperties.getAccessKey(), qiNiuProperties.getSecretKey());
		cfg = new Configuration(Zone.zone2());
	}

	public String getUpToken() {
		return auth.uploadToken(qiNiuProperties.getBucket(), null, 3600, new StringMap().put("insertOnly", 1));
	}

	public String upload(FileInputStream file, String key) throws IOException {
		

		UploadManager uploadManager = new UploadManager(cfg);
		try {
			Auth auth = Auth.create(qiNiuProperties.getAccessKey(), qiNiuProperties.getSecretKey());
			String upToken = auth.uploadToken(qiNiuProperties.getBucket());
			try {
				Response response = uploadManager.put(file, key, upToken, null, null);
				DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
				String returnPath = qiNiuProperties.getPath() + "/" + putRet.key;
				return returnPath;
			} catch (QiniuException ex) {
				Response r = ex.response;
				System.err.println(r.toString());
				try {
					System.err.println(r.bodyString());
				} catch (QiniuException ex2) {
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	 
	// public String put64image(byte[] base64, String key) throws Exception {
	// String file64 = Base64.encodeToString(base64, 0);
	// Integer l = base64.length;
	// String url = "http://upload.qiniu.com/putb64/" + l + "/key/" +
	// UrlSafeBase64.encodeToString(key);
	// RequestBody rb = RequestBody.create(null, file64);
	// Request request = new
	// Request.Builder().url(url).addHeader("Content-Type",
	// "application/octet-stream")
	// .addHeader("Authorization", "UpToken " + getUpToken()).post(rb).build();
	// OkHttpClient client = new OkHttpClient();
	// okhttp3.Response response = client.newCall(request).execute();
	// System.out.println(response);
	// return DOMAIN + key + "?" + style;
	// }

	public void delete(String key) throws IOException {
 		BucketManager bucketManager = new BucketManager(auth,cfg);
		key = key.substring(33);
		try { // 调用delete方法移动文件
			bucketManager.delete(qiNiuProperties.getBucket(), key);
		} catch (QiniuException e) { // 捕获异常信息
			Response r = e.response;
			System.out.println(r.toString());
		}
	}
}
